package logic

import (
	"baidutieba/models/mysql"
	"baidutieba/tiebarpc/internal/svc"
	"baidutieba/tiebarpc/tiebarpc"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"strings"
)

type TieBaRateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTieBaRateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TieBaRateLogic {
	return &TieBaRateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TieBaRateLogic) TieBaRate(in *tiebarpc.TieBaRateRequest) (*tiebarpc.TieBaRateResponse, error) {
	// todo: add your logic here and delete this line
	if strings.Contains(in.Context, "草") || strings.Contains(in.Context, "妈") || strings.Contains(in.Context, "傻") {
		return nil, fmt.Errorf("当前评论含敏感词汇")
	}
	var rate mysql.Rate
	mysql.DB.Where("id =?", in.UserId).Find(&rate)
	if rate.UserId == 0 {
		return nil, fmt.Errorf("当前未登录,请登陆后评论")
	}
	ba := mysql.Rate{
		UserId:  in.UserId,
		TieBaId: in.TieBaId,
		Context: in.Context,
	}

	mysql.DB.Create(&ba)
	return &tiebarpc.TieBaRateResponse{
		RateId: int64(ba.ID),
	}, nil
}
